﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Edu.Web.Data.DAO;
using Edu.Web.Data.DV;
using Edu.Web.Data.Entity;
using ESoft.Core.Data.DAO;
using ESoft.Core.Data.Service.Impl;
using ESoft.Core.Interceptor;

namespace Edu.Web.Data.Service.Impl
{
    public class TimeTableService : BaseService<TimeTableEntity, long>, ITimeTableService
    {
        private readonly ITimeTableDAO _timeTableDao;
        private readonly ITimeTableProjectDAO _tpDao;
        private readonly IClazzStuDAO _clazzStuDao;
        private readonly IClazzTeacherDAO _clazzTeacherDao;
        private readonly IClazzDAO _clazzDao;

        public TimeTableService(ITimeTableDAO dao, ITimeTableProjectDAO tpDao, IClazzStuDAO clazzStuDao, IClazzTeacherDAO clazzTeacherDao, IClazzDAO clazzDao) : base(dao)
        {
            _timeTableDao = dao;
            _tpDao = tpDao;
            _clazzStuDao = clazzStuDao;
            _clazzTeacherDao = clazzTeacherDao;
            _clazzDao = clazzDao;
        }

        public List<TimeTableDV> GetTimeTableList()
        {
            return _timeTableDao.GetTimeTableList().ToList();
        }

        [Transactional]
        public long CreateCource(TimeTableProjectEntity tpVO)
        {
            return _tpDao.Add(tpVO).Id;
        }

        public List<TimeTableCourceDV> GetTimeTableCourceList(long timeTableId)
        {
            return _timeTableDao.GetTimeTableCourceList(timeTableId).ToList();
        }

        public List<TimeTableDV> GetTimeTableListByStuId(long stuId)
        {
            var clazzIdList = _clazzStuDao.LoadEntities(x => x.StuUserId == stuId).Select(x => x.ClazzId).Distinct().ToList();
            if (clazzIdList.Any())
            {
                return _timeTableDao.GetTimeTableList().Where(x => clazzIdList.Contains(x.ClazzId)).ToList();
            }
            return new List<TimeTableDV>();
        }

        public List<TimeTableDV> GetTimeTableListByTeacherId(long teacherId)
        {
            var timeTableIds = _tpDao.LoadEntities(x => x.UserId == teacherId).Select(x => x.TimeTableId).Distinct().ToList();
            if (timeTableIds.Any())
            {
                return _timeTableDao.GetTimeTableList().Where(x => timeTableIds.Contains(x.Id)).ToList();
            }
            return new List<TimeTableDV>();
        }

        public void CancelProject(long projectId)
        {
            _tpDao.Delete(projectId);
        }

        [Transactional]
        public new void Delete(long id)
        {
            var tpList = _tpDao.LoadEntities(x => x.TimeTableId == id);
            if (tpList.Any())
                _tpDao.Delete(tpList);
            var timeTable = Find(id);
            if (timeTable != null)
            {
                Delete(timeTable);
            }
        }

        public List<CourceProjectDV> GetCourceProjectListByTeacherId(long teacherId)
        {
            

            return _timeTableDao.GetCourceProjectList()
                .Where(x => x.UserId != null && x.UserId == teacherId).Distinct().ToList();
        }

        public List<ClazzListItemDV> GetClazzListByCourceId(long courceId,long userId)
        {
            var clazzIdList = _timeTableDao.GetCourceProjectList().Where(x => x.CourceId == courceId&&x.UserId==userId).Select(x => x.ClazzId).Distinct().ToList();
            if (!clazzIdList.Any())
            {
                return new List<ClazzListItemDV>();
            }
            return _clazzDao.LoadEntities(x => clazzIdList.Contains(x.Id)).Select(x => new ClazzListItemDV { ClazzId = x.Id, ClazzName = x.ClazzName }).ToList();
        }

        public List<CourceStudentDV> GetCourceStudentList(long courceId)
        {
            return _timeTableDao.GetCourceStudentList().Where(x => x.CourceId == courceId).OrderBy(x => x.StuNo).ToList();
        }

        public List<CourceProjectDV> GetCourceProjectListByStuId(long stuId)
        {
            var clazzIds = _clazzDao.LoadDataByStuUid(stuId).Select(x => x.ClazzId).ToList();
            if (!clazzIds.Any()) return new List<CourceProjectDV>();
            return _timeTableDao.GetCourceProjectList().Where(x => clazzIds.Contains(x.ClazzId)).OrderBy(x => x.CourceId).ToList();
        }
    }
}